 package com.pansky.integration.common.utils;
 
 import java.util.Arrays;
 import java.util.Date;
 import org.apache.commons.lang3.time.DateFormatUtils;
 
 public class TimeUtils
 {
   public static final int SECOND = 0;
   public static final int MINUTE = 1;
   public static final int HOUR = 2;
   public static final int DAY = 3;
   private final int[] maxFields = { 59, 59, 23, 2147483646 };
 
   private final int[] minFields = { 0, 0, 0, -2147483648 };
 
   private String timeSeparator = ":";
 
   private int[] fields = new int[4];
 
   public static String toTimeString(long time)
   {
     TimeUtils t = new TimeUtils(time);
     int day = t.get(3);
     int hour = t.get(2);
     int minute = t.get(1);
     int second = t.get(0);
     StringBuilder sb = new StringBuilder();
     if (day > 0) {
       sb.append(day).append("天");
     }
     if (hour > 0) {
       sb.append(hour).append("时");
     }
     if (minute > 0) {
       sb.append(minute).append("分");
     }
     if (second > 0) {
       sb.append(second).append("秒");
     }
     return sb.toString();
   }
 
   public TimeUtils()
   {
     this(0, 0, 0, 0);
   }
 
   public TimeUtils(int hour, int minute)
   {
     this(0, hour, minute, 0);
   }
 
   public TimeUtils(int hour, int minute, int second)
   {
     this(0, hour, minute, second);
   }
 
   public TimeUtils(String time)
   {
     this(time, null);
   }
 
   public TimeUtils(long time)
   {
     this(new Date(time));
   }
 
   public TimeUtils(Date date)
   {
     this(DateFormatUtils.formatUTC(date, "HH:mm:ss"));
   }
 
   public TimeUtils(int day, int hour, int minute, int second)
   {
     initialize(day, hour, minute, second);
   }
 
   public TimeUtils(String time, String timeSeparator)
   {
     if (timeSeparator != null) {
       setTimeSeparator(timeSeparator);
     }
     parseTime(time);
   }
 
   public void set(int field, int value)
   {
     if (value < this.minFields[field]) {
       throw new IllegalArgumentException(new StringBuilder().append(value).append(", time value must be positive.").toString());
     }
     this.fields[field] = (value % (this.maxFields[field] + 1));
 
     int carry = value / (this.maxFields[field] + 1);
     if (carry > 0) {
       int upFieldValue = get(field + 1);
       set(field + 1, upFieldValue + carry);
     }
   }
 
   public int get(int field)
   {
     if ((field < 0) || (field > this.fields.length - 1)) {
       throw new IllegalArgumentException(new StringBuilder().append(field).append(", field value is error.").toString());
     }
     return this.fields[field];
   }
 
   public TimeUtils addTime(TimeUtils time)
   {
     TimeUtils result = new TimeUtils();
     int up = 0;
     for (int i = 0; i < this.fields.length; i++) {
       int sum = this.fields[i] + time.fields[i] + up;
       up = sum / (this.maxFields[i] + 1);
       result.fields[i] = (sum % (this.maxFields[i] + 1));
     }
     return result;
   }
 
   public TimeUtils subtractTime(TimeUtils time)
   {
     TimeUtils result = new TimeUtils();
     int down = 0;
     int i = 0; for (int k = this.fields.length - 1; i < k; i++) {
       int difference = this.fields[i] + down;
       if (difference >= time.fields[i]) {
         difference -= time.fields[i];
         down = 0;
       } else {
         difference += this.maxFields[i] + 1 - time.fields[i];
         down = -1;
       }
       result.fields[i] = difference;
     }
     result.fields[3] = (this.fields[3] - time.fields[3] + down);
     return result;
   }
 
   public String getTimeSeparator()
   {
     return this.timeSeparator;
   }
 
   public void setTimeSeparator(String timeSeparator)
   {
     this.timeSeparator = timeSeparator;
   }
 
   private void initialize(int day, int hour, int minute, int second) {
     set(3, day);
     set(2, hour);
     set(1, minute);
     set(0, second);
   }
 
   private void parseTime(String time) {
     if (time == null) {
       initialize(0, 0, 0, 0);
       return;
     }
     String t = time;
     int field = 3;
     set(field--, 0);
     int p = -1;
     while ((p = t.indexOf(this.timeSeparator)) > -1) {
       parseTimeField(time, t.substring(0, p), field--);
       t = t.substring(p + this.timeSeparator.length());
     }
     parseTimeField(time, t, field--);
   }
 
   private void parseTimeField(String time, String t, int field) {
     if ((field < 0) || (t.length() < 1)) {
       parseTimeException(time);
     }
     char[] chs = t.toCharArray();
     int n = 0;
     for (int i = 0; i < chs.length; i++)
       if (chs[i] > ' ')
       {
         if ((chs[i] >= '0') && (chs[i] <= '9')) {
           n = n * 10 + chs[i] - 48;
         }
         else
           parseTimeException(time);
       }
     set(field, n);
   }
 
   private void parseTimeException(String time) {
     throw new IllegalArgumentException(new StringBuilder().append(time).append(", time format error, HH").append(this.timeSeparator).append("mm").append(this.timeSeparator).append("ss").toString());
   }
 
   public String toString()
   {
     StringBuilder sb = new StringBuilder(16);
     sb.append(this.fields[3]).append(',').append(' ');
     buildString(sb, 2).append(this.timeSeparator);
     buildString(sb, 1).append(this.timeSeparator);
     buildString(sb, 0);
     return sb.toString();
   }
 
   private StringBuilder buildString(StringBuilder sb, int field) {
     if (this.fields[field] < 10) {
       sb.append('0');
     }
     return sb.append(this.fields[field]);
   }
 
   public int hashCode() {
     int PRIME = 31;
     int result = 1;
     result = 31 * result + Arrays.hashCode(this.fields);
     return result;
   }
 
   public boolean equals(Object obj) {
     if (this == obj)
       return true;
     if (obj == null)
       return false;
     if (getClass() != obj.getClass())
       return false;
     TimeUtils other = (TimeUtils)obj;
     if (!Arrays.equals(this.fields, other.fields)) {
       return false;
     }
     return true;
   }
 }




